package dblog.article.dao;


import dblog.article.Article;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Service
public class ArticleDAOImpl implements ArticleDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Transactional
	public void addArticle(Article article) {

		sessionFactory.getCurrentSession().save(article);

	}

	@Transactional
	public List<Article> getAllArticles() {
		List<Article> arts =  sessionFactory.getCurrentSession().createQuery("from Article").list();
		return arts;

	}

	@Transactional
	public void removeArticle(Long articleId) {
		Article article = (Article) sessionFactory.getCurrentSession().load(Article.class,articleId);
		if(article != null){
			sessionFactory.getCurrentSession().delete(article);
		}
	}
}
